package io.renren.modules.news.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.logging.Handler;

import com.baidu.ueditor.ActionEnter;
import com.baidu.ueditor.controller.UeditorController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.news.entity.NewsEntity;
import io.renren.modules.news.service.NewsService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-10-24 18:46:40
 */
@RestController
@RequestMapping("/news")
public class NewsController {
	@Autowired
	private NewsService newsService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("news:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<NewsEntity> newsList = newsService.queryList(query);
		int total = newsService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(newsList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("news:info")
	public R info(@PathVariable("id") Long id){
		NewsEntity news = newsService.queryObject(id);
		
		return R.ok().put("news", news);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("news:save")
	public R save(@RequestBody NewsEntity news){
		newsService.save(news);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("news:update")
	public R update(@RequestBody NewsEntity news){
		newsService.update(news);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("news:delete")
	public R delete(@RequestBody Long[] ids){
		newsService.deleteBatch(ids);
		return R.ok();
	}



}
